package com.yuyang.wifi.activitys.mine;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuyang.wifi.R;
import com.yuyang.wifi.activitys.NewMainHomeActivity;
import com.yuyang.wifi.base.BaseActivity;
import com.yuyang.wifi.base.BaseResult;
import com.yuyang.wifi.constant.AppConstant;
import com.yuyang.wifi.jobservice.AppService;
import com.yuyang.wifi.model.TaskDtlModel;
import com.yuyang.wifi.model.TaskDtlResult;
import com.yuyang.wifi.service.LiveWallpaperService;
import com.yuyang.wifi.utils.ActivityAllManager;
import com.yuyang.wifi.utils.DeviceChargeUtils;
import com.yuyang.wifi.utils.DeviceUtil;
import com.yuyang.wifi.utils.GetIPAdsUtil;
import com.yuyang.wifi.utils.GetMacAdsUtil;
import com.yuyang.wifi.utils.PreferencesProviderUtils;
import com.yuyang.wifi.utils.RxUtil;
import com.yuyang.wifi.utils.SpUtil;
import com.yuyang.wifi.utils.ToastUtils;
import com.yuyang.wifi.utils.eventbus.BaseEventManager;
import com.yuyang.wifi.utils.network.BaseDataSubscriber;
import com.yuyang.wifi.utils.network.HttpErrorHandler;
import com.yuyang.wifi.utils.network.HttpManager;
import com.yuyang.wifi.utils.network.RxDataInstance;
import com.yuyang.wifi.views.LoadCsjMsgUtils;
import com.yuyang.wifi.views.LoadCsjNewInsertFullExtAdUtils;
import com.yuyang.wifi.views.LoadYLHJiliVideoExtUtils;
import com.yuyang.wifi.views.LoadYlhInsertAdUtils;
import com.yuyang.wifi.views.LoadYlhMBMsgAdUtils;
import com.yuyang.wifi.views.MittUtils;

import org.jcodec.movtool.Remux;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import butterknife.BindView;
import butterknife.OnClick;
import pl.droidsonroids.gif.GifImageView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class LockScreenActivity extends BaseActivity {

    @BindView(R.id.gif_cool)
    GifImageView gifCool;
    @BindView(R.id.gif_clearing)
    GifImageView gifClearing;
    @BindView(R.id.express_container)
    FrameLayout mExpressContainer;
    @BindView(R.id.ll_task_gif)
    LinearLayout llTaskGif;
    @BindView(R.id.ll_total_layout)
    LinearLayout llTotalLayout;
    @BindView(R.id.ll_charge_layout)
    LinearLayout llChargeLayout;
    @BindView(R.id.ll_speed_layout)
    LinearLayout llSpeedLayout;
    @BindView(R.id.ll_clear_layout)
    LinearLayout llClearLayout;
    @BindView(R.id.tv_charge_percent)
    TextView tvChargePercent;
    @BindView(R.id.tv_charged_time)
    TextView tvChargedTime;
    @BindView(R.id.tv_charging_slow)
    TextView tvChargingSlow;
    @BindView(R.id.tv_content)
    TextView tvContent;//应用安装/卸载

    @BindView(R.id.express_clear_container)
    FrameLayout expressClearContainer;
    @BindView(R.id.gif_clearing_lock)
    GifImageView gifClearingLock;
    @BindView(R.id.rl_clear_finish_layout)
    RelativeLayout rlClearFinishLayout;
    @BindView(R.id.rl_clear_before_Lock_layout)
    RelativeLayout rlClearBeforeLayout;

    @BindView(R.id.express_speed_container)
    FrameLayout expressSpeedContainer;
    @BindView(R.id.gif_speeding)
    GifImageView gifSpeeding;
    @BindView(R.id.rl_speed_finish_layout)
    RelativeLayout rlSpeedFinishLayout;
    @BindView(R.id.rl_speed_before_layout)
    RelativeLayout rlSpeedBeforeLayout;

    @BindView(R.id.express_charge_container)
    FrameLayout expressChargeContainer;

    private int adFactoryType;
    private int intervaltime;
    private int tasktype;
    private String mServiceType;
    private int type;
    private String mLockType;
    private String mboardType;
    private LoadCsjNewInsertFullExtAdUtils newInsertFullAd;
    private LoadYLHJiliVideoExtUtils loadYLHJiliVideoUtils;
    private LoadYlhInsertAdUtils loadYlhInsertAdUtils;
    private String mOaid;
    private InnerRecevier innerReceiver;

    @Override
    protected void initActionBar() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lock_screen;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
            // XLog.i(XLog.BASE, "onCreate fixOrientation when Oreo, result = " + result);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initContentView() {

        if (null != getIntent()) {
            mServiceType = getIntent().getStringExtra("mServiceType");
            mLockType = getIntent().getStringExtra("mLockType");
            mboardType = getIntent().getStringExtra("board_type");
            Log.e("传参数前", "大类==" + mLockType + "  小类==" + mboardType);
            if (TextUtils.isEmpty(mLockType)) {
                mLockType = "1";
            }
            if (TextUtils.isEmpty(mboardType)) {
                mboardType = "1";
            }
            Log.e("传参数后", "大类==" + mLockType + "  小类==" + mboardType);
            Log.e("TAG", "CreateAdType==" + "广播触发类型==" + mLockType);
        }
        // ToastUtils.showShortToast(LockScreenActivity.this, "我到广告页了" + mLockType);
        Log.e("TAG", "appService==" + "lockScreenActivity");
        getAdTypeInfo();

        registerBroadcastReceiver();
    }

    private void registerBroadcastReceiver() {
        Log.e("broadReceive====", "onRegister: ===onResume");
        //创建广播
        innerReceiver = new InnerRecevier();
        //动态注册广播
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        //启动广播
        getApplicationContext().registerReceiver(innerReceiver, intentFilter);
    }

    private class InnerRecevier extends BroadcastReceiver {

        final String SYSTEM_DIALOG_REASON_KEY = "reason";

        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {

            if (null != intent) {
                String action = intent.getAction();
                if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                    String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                    if (reason != null) {
                        if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS) || reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)
                                || reason.equals("fs_gesture")) {
                            ActivityAllManager.getInstance().finishAllActivity();
                            //Toast.makeText(SignInRedBagActivity.this, "Home键被监听", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        }
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            // XLog.i(XLog.BASE, "avoid calling setRequestedOrientation when Oreo.");
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }


    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    private boolean fixOrientation() {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void getAdTypeInfo() {
        mOaid = SpUtil.find(AppConstant.KEY_OAID);
        if (TextUtils.isEmpty(mOaid)) {
            new MittUtils().getDeviceIds(this, new MittUtils.AppIdsUpdater() {
                @Override
                public void OnIdsAvailed(boolean isSupport, String oaid) {
                    SpUtil.saveOrUpdate(AppConstant.KEY_OAID, oaid);
                    mOaid = oaid;
                    Log.e("参数", "oaid:=== " + oaid);
                }
            });
        }

        Log.e("TAG", "MyApplication==" + "任务接口发起请求");

        String mac = GetMacAdsUtil.getMac(LockScreenActivity.this);
        String ipAddress = GetIPAdsUtil.getIPAddress(LockScreenActivity.this);
        String androidid = getAndroidId(LockScreenActivity.this);
        String imei = getDeviceIds(LockScreenActivity.this);
        RxDataInstance.getInstance().initMap(LockScreenActivity.this)
                .put("imei", imei)
                .put("oaid", mOaid)
                .put("idfa", "")
                .put("androidid", androidid)
                .put("mac", mac)
                .put("ip", ipAddress)
                .put("scene", "after")
                .put("bigtype", mLockType);
        HttpManager.getInstance().getApi().getTaskDtl(RxDataInstance.getInstance().getParamMap())
                .compose(RxUtil.<TaskDtlResult>bindLifecycleAndApplySchedulers(this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseDataSubscriber<TaskDtlResult>(new HttpErrorHandler() {
                    @Override
                    public void onHttpError(BaseResult result) {
                        ActivityAllManager.getInstance().finishAllActivity();
                        // ToastUtils.showShortToast(LockScreenActivity.this, result.getErrorMessage());
                    }

                    @Override
                    public void onHttpException(Throwable e) {
                        // dismissLoadingDialog();
                    }
                }) {
                    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void onDataNext(TaskDtlResult result) {
                        int resultCode = result.getResultCode();
                        if (0 == resultCode) {
                            TaskDtlModel data = result.getData();
                            if (null != data) {
                                tasktype = data.getTasktype();
                                adFactoryType = data.getAdvertiser();
                                intervaltime = data.getIntervaltime();
                                //1:亮屏 2：插电|拔电 3：WIFI连接|断开 4：安装|卸载
                                type = data.getType();
                                //ToastUtils.showShortToast(LockScreenActivity.this, "接口ok,我到广告类型" + type);
                                if (type == 1 || type == 5) {
                                    llTotalLayout.setVisibility(View.VISIBLE);
                                    llTaskGif.setVisibility(View.VISIBLE);
                                    if (adFactoryType == 1) {
                                        showMsg(mExpressContainer, "csj_msg_lock");
                                    } else {
                                        showYlhMsg(mExpressContainer, "ylh_msg_lock");
                                    }
                                    if (tasktype == 1) {
                                        gifCool.setImageResource(R.drawable.lock_one_key_clear);
                                    } else if (tasktype == 2) {
                                        //全屏
                                        gifCool.setImageResource(R.drawable.lock_one_key_speed);
                                    } else if (tasktype == 3) {
                                        //插屏
                                        gifCool.setImageResource(R.drawable.lock_one_key_cool);
                                    }
                                    setFirstTask();
                                } else if (type == 2) {
                                    //插电|拔电
                                    showChargeView();
                                    if (adFactoryType == 1) {
                                        if (mboardType.equals("2")) {
                                            showMsg(expressChargeContainer, "csj_msg_chadian_lock");
                                        } else {
                                            showMsg(expressChargeContainer, "csj_msg_duandian_lock");
                                        }
                                    } else {
                                        if (mboardType.equals("2")) {
                                            showYlhMsg(expressChargeContainer, "ylh_msg_chadian_lock");
                                        } else {
                                            showYlhMsg(expressChargeContainer, "ylh_msg_duandian_lock");
                                        }
                                    }

                                    llChargeLayout.setVisibility(View.VISIBLE);
                                } else if (type == 3) {
                                    //3：WIFI连接|断开
                                    if (adFactoryType == 1) {
                                        showMsg(expressSpeedContainer, "csj_msg_wifi_lianjie_lock");
                                    } else {
                                        showYlhMsg(expressSpeedContainer, "ylh_msg_wifi_lianjie_lock");
                                    }
                                    llSpeedLayout.setVisibility(View.VISIBLE);
                                } else if (type == 4) {
                                    //4：安装|卸载
                                    if (adFactoryType == 1) {
                                        if (mboardType.equals("4")) {
                                            tvContent.setText("检测到您已安装新应用\n可能残留垃圾，是否清理");
                                            showMsg(expressClearContainer, "csj_msg_anzhuang_lock");
                                        } else {
                                            tvContent.setText("检测到您已卸载应用\n可能残留垃圾，是否清理");
                                            showMsg(expressClearContainer, "csj_msg_xiezai_lock");
                                        }
                                    } else {
                                        if (mboardType.equals("4")) {
                                            tvContent.setText("检测到您已安装新应用\n可能残留垃圾，是否清理");
                                            showYlhMsg(expressClearContainer, "ylh_msg_anzhuang_lock");
                                        } else {
                                            tvContent.setText("检测到您已卸载应用\n可能残留垃圾，是否清理");
                                            showYlhMsg(expressClearContainer, "ylh_msg_xiezai_lock");
                                        }
                                    }
                                    llClearLayout.setVisibility(View.VISIBLE);
                                }
                            } else {
                                Log.e("锁屏data", "  ==" + "null");
                                // ActivityAllManager.getInstance().finishAllActivity();
                                finish();
                            }
                        } else {
                            // ToastUtils.showShortToast(LockScreenActivity.this, result.getErrorMessage());
                        }
                    }
                });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void showChargeView() {
        int Level = DeviceChargeUtils.getCurrentChargeLevel(this);
        tvChargePercent.setText(Level + "%");
        if (Level < 100) {
            int ss = 100 - Level + 2;
            tvChargedTime.setText(ss + "分钟后充满");
        } else {
            tvChargedTime.setText("当前电量已充满");
            tvChargingSlow.setVisibility(View.GONE);
        }
    }

    private void showFullAd(int insertType) {
        if (insertType == 1) {
            newInsertFullAd = LoadCsjNewInsertFullExtAdUtils.getInstance();
            newInsertFullAd.load(LockScreenActivity.this, "csj_jl_lock_first");

            loadYLHJiliVideoUtils = LoadYLHJiliVideoExtUtils.getInstance();
            loadYLHJiliVideoUtils.load(LockScreenActivity.this, "ylh_jl_lock_first");
        } else if (insertType == 2) {
            newInsertFullAd = LoadCsjNewInsertFullExtAdUtils.getInstance();
            newInsertFullAd.load(LockScreenActivity.this, "csj_jl_lock_second");

            loadYLHJiliVideoUtils = LoadYLHJiliVideoExtUtils.getInstance();
            loadYLHJiliVideoUtils.load(LockScreenActivity.this, "ylh_jl_lock_second");
        } else if (insertType == 3) {
            newInsertFullAd = LoadCsjNewInsertFullExtAdUtils.getInstance();
            newInsertFullAd.load(LockScreenActivity.this, "csj_insert_lock");

            loadYlhInsertAdUtils = LoadYlhInsertAdUtils.getInstance();

        }
        if (adFactoryType == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    newInsertFullAd.showNewInterAd();
                    newInsertFullAd.setLoadFailureListener(new LoadCsjNewInsertFullExtAdUtils.OnLoadFailureListener() {
                        @Override
                        public void onLoadFailureListener() {
                            finish();
                        }
                    });
                }
            }, 1000);
            newInsertFullAd.setItemAllClickListener(new LoadCsjNewInsertFullExtAdUtils.OnItemAllClickListener() {
                @Override
                public void onItemAllClickListener() {
                    try {
                        switch (tasktype) {
                            case 1:
                                gifClearing.setImageResource(R.drawable.lock_clear_finish);
                                break;
                            case 2:
                                gifClearing.setImageResource(R.drawable.lock_speed_finish);
                                break;
                            case 3:
                                gifClearing.setImageResource(R.drawable.lock_cool_finish);
                                break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } else if (adFactoryType == 2) {
            //插屏
            if (null != loadYlhInsertAdUtils) {
                loadYlhInsertAdUtils.load(LockScreenActivity.this, "ylh_insert_lock");
                //loadYlhInsertAdUtils.showInterActionAdVideo();
                loadYlhInsertAdUtils.setItemAllClickListener(new LoadYlhInsertAdUtils.OnItemAllClickListener() {
                    @Override
                    public void onItemAllClickListener() {
                        gifClearing.setImageResource(R.drawable.lock_cool_finish);
                    }
                });
            }
            //激励视频
            if (null != loadYLHJiliVideoUtils) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadYLHJiliVideoUtils.play();
                        loadYLHJiliVideoUtils.setLoadFailureListener(new LoadYLHJiliVideoExtUtils.OnLoadFailureListener() {
                            @Override
                            public void onLoadFailureListener() {
                                finish();
                            }
                        });
                    }
                }, 1000);
                loadYLHJiliVideoUtils.setAdCloseListener(new LoadYLHJiliVideoExtUtils.OnAdCloseListener() {
                    @Override
                    public void onAdCloseListenerListener(boolean isPlayComplete) {
                        try {
                            switch (tasktype) {
                                case 1:
                                    gifClearing.setImageResource(R.drawable.lock_clear_finish);
                                    break;
                                case 2:
                                    gifClearing.setImageResource(R.drawable.lock_speed_finish);
                                    break;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }

    private void setFirstTask() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    llTaskGif.setVisibility(View.GONE);
                    gifClearing.setVisibility(View.VISIBLE);
                    llTotalLayout.setBackgroundResource(0);
                    switch (tasktype) {
                        case 1:
                            gifClearing.setImageResource(R.drawable.bg_lock_clearing);
                            break;
                        case 2:
                            gifClearing.setImageResource(R.drawable.bg_lock_speeding);
                            break;
                        case 3:
                            gifClearing.setImageResource(R.drawable.bg_lock_cooling);
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 4800);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //llTaskGif.setVisibility(View.GONE);
                switch (tasktype) {
                    case 1:
                        showFullAd(1);
                        break;
                    case 2:
                        showFullAd(2);
                        break;
                    case 3:
                        showFullAd(3);
                        break;
                }
            }
        }, 6200);
    }

    private void nextAction() {
        if (!TextUtils.isEmpty(mServiceType) && "1".equals(mServiceType)) {
            AppService appService = new AppService();
            //AppService appService = AppService.getInstance();
            Log.e("url===", "nextAction:time=== " + intervaltime);
            appService.lunchAC(this, intervaltime, mLockType, mboardType);
        } else {
            LiveWallpaperService liveWallpaperService = new LiveWallpaperService();
            liveWallpaperService.lunchAC(intervaltime, mLockType, mboardType);
        }
    }

    @OnClick({R.id.tv_speed_one_key, R.id.iv_speed_top, R.id.tv_speed_finish, R.id.iv_clear_close, R.id.iv_speed_close,
            R.id.tv_clear_one_key, R.id.tv_charge_close, R.id.gif_clearing})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_speed_one_key:
                showSpeedNextAction();
                break;
            case R.id.tv_clear_one_key:
                showClearNextAction();
                break;
            case R.id.iv_speed_top:

                break;
            case R.id.iv_clear_close:
            case R.id.iv_speed_close:
            case R.id.tv_charge_close:
                finish();
                break;
            case R.id.gif_clearing:
                Log.e("TAG", "下个任务=== " + intervaltime);
                // nextAction();
                finish();
                break;
        }
    }

    private void showClearNextAction() {
        rlClearBeforeLayout.setVisibility(View.GONE);
        rlClearFinishLayout.setVisibility(View.GONE);
        expressClearContainer.setVisibility(View.GONE);
        gifClearingLock.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (adFactoryType == 1) {
                    //全屏广告
                    newInsertFullAd = LoadCsjNewInsertFullExtAdUtils.getInstance();
                    newInsertFullAd.load(LockScreenActivity.this, "csj_jl_clear_lock");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            newInsertFullAd.showNewInterAd();
                        }
                    }, 1000);
                    newInsertFullAd.setItemAllClickListener(new LoadCsjNewInsertFullExtAdUtils.OnItemAllClickListener() {
                        @Override
                        public void onItemAllClickListener() {
                            try {
                                showMsg(expressClearContainer, "csj_msg_clear_finish_lock");
                                gifClearingLock.setVisibility(View.GONE);
                                rlClearBeforeLayout.setVisibility(View.GONE);
                                rlClearFinishLayout.setVisibility(View.VISIBLE);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    expressClearContainer.setVisibility(View.VISIBLE);
                    loadYLHJiliVideoUtils = loadYLHJiliVideoUtils.getInstance();
                    loadYLHJiliVideoUtils.load(LockScreenActivity.this, "ylh_jl_clear_lock");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            loadYLHJiliVideoUtils.play();
                            loadYLHJiliVideoUtils.setLoadFailureListener(new LoadYLHJiliVideoExtUtils.OnLoadFailureListener() {
                                @Override
                                public void onLoadFailureListener() {
                                    finish();
                                }
                            });
                        }
                    }, 1000);
                    loadYLHJiliVideoUtils.setAdCloseListener(new LoadYLHJiliVideoExtUtils.OnAdCloseListener() {
                        @Override
                        public void onAdCloseListenerListener(boolean isPlayComplete) {
                            showYlhMsg(expressClearContainer, "ylh_msg_clear_finish_lock");
                            gifClearingLock.setVisibility(View.GONE);
                            rlClearBeforeLayout.setVisibility(View.GONE);
                            rlClearFinishLayout.setVisibility(View.VISIBLE);
                        }
                    });
                }


            }
        }, 3000);
    }

    private void showSpeedNextAction() {
        rlSpeedBeforeLayout.setVisibility(View.GONE);
        rlSpeedFinishLayout.setVisibility(View.GONE);
        expressSpeedContainer.setVisibility(View.GONE);
        gifSpeeding.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (adFactoryType == 1) {
                    newInsertFullAd = LoadCsjNewInsertFullExtAdUtils.getInstance();
                    newInsertFullAd.load(LockScreenActivity.this, "csj_jl_speed_lock");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            newInsertFullAd.showNewInterAd();
                        }
                    }, 1000);
                    newInsertFullAd.setItemAllClickListener(new LoadCsjNewInsertFullExtAdUtils.OnItemAllClickListener() {
                        @Override
                        public void onItemAllClickListener() {
                            showMsg(expressSpeedContainer, "csj_msg_speed_finish_lock");
//                        if (mboardType.equals("4")) {
//                            showMsg(expressSpeedContainer, "csj_msg_speed_finish_lock");
//                        } else {
//                            showMsg(expressSpeedContainer, "csj_msg_speed_finish_lock");
//                        }
                            gifSpeeding.setVisibility(View.GONE);
                            rlSpeedBeforeLayout.setVisibility(View.GONE);
                            rlSpeedFinishLayout.setVisibility(View.VISIBLE);
                        }
                    });
                } else {
                    expressSpeedContainer.setVisibility(View.VISIBLE);
                    loadYLHJiliVideoUtils = loadYLHJiliVideoUtils.getInstance();
                    loadYLHJiliVideoUtils.load(LockScreenActivity.this, "ylh_jl_speed_lock");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            loadYLHJiliVideoUtils.play();
                        }
                    }, 1000);
                    loadYLHJiliVideoUtils.setAdCloseListener(new LoadYLHJiliVideoExtUtils.OnAdCloseListener() {
                        @Override
                        public void onAdCloseListenerListener(boolean isPlayComplete) {
                            showYlhMsg(expressSpeedContainer, "ylh_msg_speed_finish_lock");
//                        if (mboardType.equals("4")) {
//                            showMsg(expressSpeedContainer, "csj_msg_speed_finish_lock");
//                        } else {
//                            showMsg(expressSpeedContainer, "csj_msg_speed_finish_lock");
//                        }
                            gifSpeeding.setVisibility(View.GONE);
                            rlSpeedBeforeLayout.setVisibility(View.GONE);
                            rlSpeedFinishLayout.setVisibility(View.VISIBLE);
                        }
                    });
                }

            }
        }, 3000);
    }

    private void showMsg(FrameLayout containr, String msgId) {
        int screenWidth = DeviceUtil.getWidth(LockScreenActivity.this) - 100;
        Log.e("TAG", "showMsg:=== " + screenWidth);
        LoadCsjMsgUtils csjMsgInstance = LoadCsjMsgUtils.getInstance();
        csjMsgInstance.load(LockScreenActivity.this, containr,
                320, 287, msgId);
    }

    private void showYlhMsg(FrameLayout containr, String msgId) {
        LoadYlhMBMsgAdUtils loadYlhMBMsgAdUtils = LoadYlhMBMsgAdUtils.getInstance();
        loadYlhMBMsgAdUtils.load(LockScreenActivity.this, containr, msgId);
        loadYlhMBMsgAdUtils.showInterActionAdVideo();
    }

    public static boolean isHarmonyOs() {
        try {
            Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
            Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
            return "harmony".equalsIgnoreCase(osBrand.toString());
        } catch (Throwable e) {
            return false;
        }
    }

    private static String getAndroidId(Context context) {
        String deviceID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (!TextUtils.isEmpty(deviceID)) {
            return deviceID;
        }
        return deviceID;
    }

    //获取设备ID
    public static String getDeviceIds(Context context) {

        try {
            TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyMgr != null) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    return "";
                }
                String deviceId = telephonyMgr.getDeviceId();
                //Log.e("channel", "onViewClicked:" + deviceId);
                return deviceId;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return "";
    }
}
